package com.lyf.boot.core.model;

import com.lyf.boot.constant.HttpStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author Luyufan
 * @date 2023/7/3 11:49
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -2463075265815240521L;

    public final static String DEFAULT_SUCCESS_MESSAGE = "操作成功";

    private final static String DEFAULT_FAIL_MESSAGE = "操作失败";

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 消息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    public static R<Void> ok(){
        R<Void> res = new R<>();
        res.setCode(200);
        res.setMessage(DEFAULT_SUCCESS_MESSAGE);
        return res;
    }

    public static <T> R<T> ok(T data){
        R<T> res = new R<>();
        res.setCode(200);
        res.setMessage(DEFAULT_SUCCESS_MESSAGE);
        res.setData(data);
        return res;
    }

    public static <T> R<T> ok(String message, T data){
        return ok(data).setMessage(message);
    }

    public static <T> R<T> fail(){
        return fail(DEFAULT_FAIL_MESSAGE);
    }

    public static <T> R<T> fail(String message){
        return fail(HttpStatus.ERROR, message);
    }

    public static <T> R<T> fail(Integer code, String message){
        R<T> res = new R<>();
        res.setCode(code);
        res.setMessage(message);
        return res;
    }

}
